<!--
 * 用户信息
 *
 * @Author: ShanZhu
 * @Date: 2023-12-03
-->
<template>
  <div class="scifi-userinfo-container">
    <!-- 动态背景元素 -->
    <div class="scifi-bg-elements">
      <div class="floating-orb orb-1"></div>
      <div class="floating-orb orb-2"></div>
      <div class="floating-orb orb-3"></div>
      <div class="grid-lines"></div>
    </div>

    <div class="scifi-content-wrapper">
      <div class="scifi-card">
        <!-- 标题区域 -->
        <div class="scifi-header">
          <div class="logo-container">
            <div class="hologram-logo">
              <div class="logo-core"></div>
              <div class="logo-ring"></div>
            </div>
          </div>
          <h1 class="scifi-title">个人信息</h1>
          <p class="scifi-subtitle">用户信息管理系统</p>
        </div>

        <el-row :gutter="20" class="scifi-action-panel">
          <el-col :span="24">
            <div class="grid-content bg-purple">
              <el-button
                  type="primary"
                  @click="updateUser"
                  class="scifi-btn edit-btn"
              >
                <div class="btn-content">
                  <i class="iconfont icon-edit-button"></i>
                  <span>修改信息</span>
                </div>
                <div class="btn-glow"></div>
              </el-button>
            </div>
          </el-col>
        </el-row>

        <div class="scifi-divider"></div>

        <!-- 基本信息区域 -->
        <div class="scifi-info-panel">
          <h2 class="scifi-panel-title">个人基本信息</h2>
          <div class="info-grid">
            <div class="info-item">
              <label class="info-label">用户名：</label>
              <span class="info-value">{{ userInfo.username }}</span>
            </div>
            <div class="info-item">
              <label class="info-label">姓名：</label>
              <span class="info-value">{{ userInfo.nike }}</span>
            </div>
            <div class="info-item">
              <label class="info-label">联系电话：</label>
              <span class="info-value">{{ userInfo.phone }}</span>
            </div>
            <div class="info-item">
              <label class="info-label">年龄：</label>
              <span class="info-value">{{ userInfo.age }}岁</span>
            </div>
            <div class="info-item">
              <label class="info-label">性别：</label>
              <span class="info-value">{{ userInfo.sex }}</span>
            </div>
            <div class="info-item">
              <label class="info-label">车牌号：</label>
              <span class="info-value">{{ userInfo.card }}</span>
            </div>
          </div>
        </div>

        <div class="scifi-divider"></div>

        <!-- 图表区域 -->
        <div class="scifi-charts-panel">
          <el-row :gutter="20">
            <el-col :span="12">
              <div v-if="!dataTypeShow" class="scifi-no-data">
                <h2 class="scifi-panel-title">停车类型记录</h2>
                <div class="no-data-text">没有相关记录</div>
              </div>
              <div
                  v-show="dataTypeShow"
                  class="scifi-chart-container"
                  id="myChart"
              ></div>
            </el-col>
            <el-col :span="12">
              <div class="scifi-chart-container" id="chart2"></div>
            </el-col>
          </el-row>
        </div>
      </div>
    </div>

    <!-- 修改信息对话框 -->
    <el-dialog
        title="修改信息"
        :visible.sync="editShow"
        width="50%"
        center
        class="scifi-dialog"
        @close="editClose"
    >
      <div class="scifi-dialog-content">
        <el-form
            :model="editUser"
            :rules="editRules"
            ref="editRules"
            label-width="100px"
            class="scifi-form"
        >
          <el-form-item label="姓名" prop="nike">
            <el-input v-model="editUser.nike" class="scifi-input"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="editUser.password" type="password" class="scifi-input"></el-input>
          </el-form-item>
          <el-form-item label="车牌号" prop="card">
            <el-input v-model="editUser.card" class="scifi-input"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="editUser.phone" class="scifi-input"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <div class="scifi-radio-group">
              <el-radio v-model="editUser.sex" label="男" class="scifi-radio">男</el-radio>
              <el-radio v-model="editUser.sex" label="女" class="scifi-radio">女</el-radio>
            </div>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
                <el-button @click="editClose()" class="scifi-btn cancel-btn">取 消</el-button>
                <el-button type="primary" @click="editCommit('editRules')" class="scifi-btn confirm-btn">
                    <div class="btn-content">
                        <i class="iconfont icon-r-yes"></i>
                        <span>确认修改</span>
                    </div>
                    <div class="btn-glow"></div>
                </el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("年龄不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (value < 18) {
            callback(new Error("必须年满18岁"));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.editUser.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      userInfo: {},
      editUser: {
        password: "",
        age: "",
        sex: "",
        phone: "",
        card: "",
        nike: "",
      },
      editShow: false,
      dataTypes: [],
      dataMoneyName: [],
      dataMoneyValue: [],
      dataTypeShow: false,
      editRules: {
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        check: [{ validator: validatePass2, trigger: "blur" }],
        age: [{ validator: checkAge, trigger: "blur" }],
        nike: [
          { required: true, message: "请输入姓名", trigger: "blur" },
        ],
        phone: [
          {
            required: true,
            message: "请输入联系电话",
            trigger: "blur",
          },
        ],
        card: [
          {
            required: true,
            message: "请输入车牌号",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    getUserInfo(uid) {
      axios.get("/api/user/getUserByUid?uid=" + uid).then((res) => {
        this.userInfo = res.data.data;
        this.getUserDataType(this.userInfo.username);
        this.getUserDataMoney(this.userInfo.username);
      });
    },
    getUserDataType(person) {
      axios.get("/api/user/getCarTypes?person=" + person).then((res) => {
        this.dataTypes = res.data.data;
        if (this.dataTypes.length > 0) {
          this.dataTypeShow = true;
          this.$nextTick(() => {
            this.draw1();
          });
        } else {
          this.dataTypeShow = false;
        }
      });
    },
    getUserDataMoney(person) {
      axios.get("/api/user/dataMoney?person=" + person).then((res) => {
        this.dataMoneyName = [];
        this.dataMoneyValue = [];
        console.log(res.data);
        if (res.data.data.length > 0) {
          for (let i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i] != null) {
              this.dataMoneyName.push(
                  this.dateFormat(res.data.data[i].name, 2)
              );
              this.dataMoneyValue.push(res.data.data[i].value);
            }
          }
          this.$nextTick(() => {
            this.draw2();
          });
        }
      });
    },
    updateUser() {
      this.editUser.phone = this.userInfo.phone;
      this.editUser.age = this.userInfo.age;
      this.editUser.sex = this.userInfo.sex;
      this.editUser.card = this.userInfo.card;
      this.editUser.password = this.userInfo.password;
      this.editUser.nike = this.userInfo.nike;
      this.editShow = true;
    },
    editClose() {
      this.editShow = false;
    },
    draw1() {
      var echarts = require("echarts");
      var myChart = echarts.init(document.getElementById("myChart"));
      var option = {
        title: {
          text: "停车类型记录",
          left: "center",
          textStyle: {
            color: '#00ffff'
          }
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left",
          textStyle: {
            color: 'rgba(255, 255, 255, 0.8)'
          }
        },
        series: [
          {
            name: "次数",
            type: "pie",
            radius: "50%",
            data: this.dataTypes,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 255, 255, 0.5)",
              },
            },
          },
        ],
        backgroundColor: 'transparent'
      };

      option && myChart.setOption(option);
    },
    draw2() {
      var echarts = require("echarts");
      var myChart = echarts.init(document.getElementById("chart2"));
      var option = {
        title: {
          text: "近五天缴费记录",
          left: "center",
          textStyle: {
            color: '#00ffff'
          }
        },
        toolbox: {
          show: true,
          orient: "vertical",
          left: "right",
          top: "center",
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            magicType: {
              show: true,
              type: ["line", "bar", "stack"],
            },
            restore: { show: true },
            saveAsImage: { show: true },
          },
        },
        xAxis: {
          type: "category",
          axisLabel: {
            rotate: 45,
            color: 'rgba(255, 255, 255, 0.8)'
          },
          data: this.dataMoneyName,
        },
        yAxis: {
          type: "value",
          axisLabel: {
            color: 'rgba(255, 255, 255, 0.8)'
          }
        },
        series: [
          {
            data: this.dataMoneyValue,
            type: "bar",
            showBackground: true,
            backgroundStyle: {
              color: "rgba(180, 180, 180, 0.2)",
            },
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: '#00ffff' },
                { offset: 1, color: '#0080ff' }
              ])
            }
          },
        ],
        backgroundColor: 'transparent'
      };

      option && myChart.setOption(option);
    },
    editCommit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.editUser.uid = this.userInfo.uid;
          axios
              .post("/api/user/updateByUser", this.editUser)
              .then((res) => {
                if (res.data.data) {
                  this.$message.success("修改成功！");
                  this.getUserInfo(this.userInfo.uid);
                } else {
                  this.$message.error("修改失败！");
                }
                this.editShow = false;
              });
        } else {
          return false;
        }
      });
    },
    //时间转换函数
    dateFormat(val, timeType) {
      let date = new Date(val);
      let resultFormat = "Y年M月D日 h:m:s";
      switch (timeType) {
        case 1:
          resultFormat = "Y年M月D日";
          break;
        case 2:
          resultFormat = "Y-M-D";
          break;
        case 3:
          resultFormat = "Y-M-D h:m:s";
          break;
      }
      let result = formatTime(resultFormat);

      function formatNumber(n) {
        n = n.toString();
        return n[1] ? n : "0" + n;
      }
      function formatTime(format) {
        let newArr = [];
        let formatArr = ["Y", "M", "D", "h", "m", "s"];
        newArr.push(date.getFullYear());
        newArr.push(formatNumber(date.getMonth() + 1));
        newArr.push(formatNumber(date.getDate()));

        newArr.push(formatNumber(date.getHours()));
        newArr.push(formatNumber(date.getMinutes()));
        newArr.push(formatNumber(date.getSeconds()));

        for (let i in newArr) {
          format = format.replace(formatArr[i], newArr[i]);
        }
        return format;
      }

      return result;
    },
  },
  mounted() {
    const uid = window.sessionStorage.getItem("token");
    this.getUserInfo(uid);
  },
};
</script>

<style scoped lang="less">
.scifi-userinfo-container {
  position: relative;
  min-height: 100vh;
  background: linear-gradient(135deg, #0c0c2e 0%, #1a1a3e 50%, #2d2d5e 100%);
  overflow-x: hidden;
  font-family: 'Arial', sans-serif;
  color: #fff;
}

.scifi-content-wrapper {
  position: relative;
  z-index: 1;
  padding: 20px;
  display: flex;
  justify-content: center;
}

.scifi-card {
  position: relative;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  padding: 30px;
  width: 95%;
  max-width: 1200px;
  box-shadow:
      0 8px 32px rgba(0, 0, 0, 0.3),
      inset 0 1px 0 rgba(255, 255, 255, 0.2),
      0 0 20px rgba(0, 255, 255, 0.1);
  animation: glow 2s ease-in-out infinite alternate;
}

.scifi-header {
  text-align: center;
  margin-bottom: 30px;
}

.scifi-title {
  color: #00ffff;
  font-size: 28px;
  font-weight: 300;
  margin-bottom: 8px;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
  letter-spacing: 2px;
}

.scifi-subtitle {
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
  margin: 0;
}

.scifi-action-panel {
  margin-bottom: 20px;
}

.edit-btn {
  background: linear-gradient(135deg, #00ffff, #0080ff);
  color: #000;

  &:hover {
    box-shadow: 0 5px 20px rgba(0, 255, 255, 0.4);
  }
}

.scifi-divider {
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(0, 255, 255, 0.5), transparent);
  margin: 25px 0;
}

.scifi-info-panel {
  margin-bottom: 30px;
}

.scifi-panel-title {
  color: #00ffff;
  font-size: 20px;
  margin-bottom: 20px;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.3);
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.info-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  transition: all 0.3s ease;

  &:hover {
    background: rgba(255, 255, 255, 0.08);
    border-color: rgba(0, 255, 255, 0.3);
    transform: translateY(-2px);
  }
}

.info-label {
  color: #00ffff;
  font-weight: 500;
  min-width: 100px;
}

.info-value {
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
}

.scifi-charts-panel {
  margin-top: 30px;
}

.scifi-chart-container {
  width: 100%;
  height: 400px;
  background: rgba(255, 255, 255, 0.02);
  border-radius: 15px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 20px;
}

.scifi-no-data {
  text-align: center;
  padding: 60px 20px;
  background: rgba(255, 255, 255, 0.02);
  border-radius: 15px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.no-data-text {
  color: rgba(255, 255, 255, 0.5);
  font-size: 18px;
  margin-top: 20px;
}

/* 对话框样式 */
.scifi-dialog {
  :deep(.el-dialog) {
    background: linear-gradient(135deg, #1a1a3e 0%, #2d2d5e 100%);
    border: 1px solid rgba(0, 255, 255, 0.3);
    border-radius: 15px;
    box-shadow: 0 0 30px rgba(0, 255, 255, 0.2);
  }

  :deep(.el-dialog__title) {
    color: #00ffff;
    font-size: 20px;
  }

  :deep(.el-dialog__header) {
    border-bottom: 1px solid rgba(0, 255, 255, 0.2);
  }
}

.scifi-form {
  :deep(.el-form-item__label) {
    color: #00ffff;
  }
}

.scifi-input {
  :deep(.el-input__inner) {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 10px;
    color: #fff;
    height: 45px;
    transition: all 0.3s ease;

    &::placeholder {
      color: rgba(255, 255, 255, 0.5);
    }

    &:focus {
      border-color: #00ffff;
      box-shadow: 0 0 15px rgba(0, 255, 255, 0.3);
      background: rgba(255, 255, 255, 0.08);
    }
  }
}

.scifi-radio-group {
  :deep(.el-radio) {
    color: rgba(255, 255, 255, 0.8);

    &.is-checked {
      .el-radio__inner {
        background: #00ffff;
        border-color: #00ffff;
      }

      .el-radio__label {
        color: #00ffff;
      }
    }
  }
}

.cancel-btn {
  background: rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.confirm-btn {
  background: linear-gradient(135deg, #00ffff, #0080ff);
  color: #000;
}

/* 按钮通用样式 */
.scifi-btn {
  position: relative;
  border: none;
  border-radius: 10px;
  padding: 12px 24px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  overflow: hidden;

  .btn-content {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .btn-glow {
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
    transition: left 0.5s ease;
  }

  &:hover {
    transform: translateY(-2px);

    .btn-glow {
      left: 100%;
    }
  }
}

/* 背景元素样式 */
.scifi-bg-elements {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.floating-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(40px);
  opacity: 0.1;
  animation: float 6s ease-in-out infinite;
}

.orb-1 {
  width: 200px;
  height: 200px;
  background: #00ffff;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.orb-2 {
  width: 150px;
  height: 150px;
  background: #ff00ff;
  top: 60%;
  right: 10%;
  animation-delay: 2s;
}

.orb-3 {
  width: 100px;
  height: 100px;
  background: #ffff00;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

.grid-lines {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image:
      linear-gradient(rgba(0, 255, 255, 0.1) 1px, transparent 1px),
      linear-gradient(90deg, rgba(0, 255, 255, 0.1) 1px, transparent 1px);
  background-size: 50px 50px;
  opacity: 0.3;
}

/* 动画 */
@keyframes glow {
  from {
    box-shadow:
        0 8px 32px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.2),
        0 0 20px rgba(0, 255, 255, 0.1);
  }
  to {
    box-shadow:
        0 8px 32px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.2),
        0 0 30px rgba(0, 255, 255, 0.3);
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) translateX(0);
  }
  50% {
    transform: translateY(-20px) translateX(10px);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .scifi-card {
    width: 95%;
    padding: 20px 15px;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }

  .scifi-chart-container {
    height: 300px;
  }
}
</style>